<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;

class ExperienceController extends CommonController {
    /*
     * 体验卡列表
     * author xfwed 
     * date 2021.05.31
     */

    public function experienceList() {
        $name = input('param.name');
        $status = input('param.status');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($name) {
            $where['name'] = ['like', '%' . $name . '%'];
        }
        if ($status) {
            $where['status'] = $status;
        }
        $where = isset($where) ? $where : [];

        $list = Db::name('super_card')
                ->where($where)
                ->field('id,name,price,special_price,if_redmoney,limit_num,end_time,sell_num,status,sell_city_name')
                ->order('id desc')
                ->paginate($perPage);

        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 添加体验卡
     * author xfwed 
     * date 2021.05.31
     */

    public function addExperience() {
        $rule = [
            'name' => 'require',
            'sell_per' => 'require',
            'end_time' => 'require',
            'price' => 'require',
            'special_price' => 'require',
            'if_redmoney' => 'require',
            'limit_num' => 'require',
            'img' => 'require',
            'note' => 'require',
            'sell_num_base' => 'require',
            'content' => 'require',
            'sell_city_name' => 'require',
        ];
        $msg = [
            'name.require' => '体验卡名称必须',
            'sell_per.require' => '销售收益比例必须',
            'end_time.require' => '体验卡有效期必须',
            'price.require' => '原价必须',
            'special_price.require' => '特权价必须',
            'if_redmoney.require' => '是否可用美容金必须',
            'limit_num.require' => '每个账号限购必须',
            'img.require' => '列表图片必须',
            'note.require' => '下单提醒必须',
            'sell_num_base.require' => '已售基数必须',
            'content.require' => '详情介绍必须',
            'sell_city_name.require' => '售卖城市名必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        Db::startTrans();
        $cardData['name'] = $all['name'];
        $cardData['sell_per'] = $all['sell_per'];
        $cardData['end_time'] = $all['end_time'];
        $cardData['price'] = $all['price'];
        $cardData['special_price'] = $all['special_price'];
        $cardData['if_redmoney'] = $all['if_redmoney'];
        $cardData['limit_num'] = $all['limit_num'];
        $cardData['img'] = $all['img'];
        $cardData['note'] = $all['note'];
        $cardData['sell_num_base'] = $all['sell_num_base'];
        $cardData['content'] = $all['content'];
        $cardData['sell_city_name'] = $all['sell_city_name'];
        $cardData['add_time'] = date('Y-m-d H:i:s');
        $insertCard = Db::name('super_card')->insertGetId($cardData);
        $a = [['item_name' => '子项目一', 'price' => 40, 'special_price' => 30, 'ser_num' => 1, 'factory_per' => 40, 'staff_per' => 20, 'store_per' => 20, 'sell_per' => 10, 'kkm_per' => 10, 'yuyueTime' => [['store_id' => 27, 'yuyue_date' => '2021-06-02', 'time1_num' => 10, 'time2_num' => 10, 'time3_num' => 10], ['store_id' => 28, 'yuyue_date' => '2021-06-02', 'time1_num' => 10, 'time2_num' => 10, 'time3_num' => 10]]]];
        //print_r(json_encode($a));die;
        //[{"item_name":"\u5b50\u9879\u76ee\u4e00","price":40,"special_price":30,"ser_num":1,"factory_per":40,"staff_per":20,"store_per":20,"sell_per":10,"kkm_per":10,"yuyueTime":[{"store_id":27,"yuyue_date":"2021-06-02","time1_num":10,"time2_num":10,"time3_num":10},{"store_id":28,"yuyue_date":"2021-06-02","time1_num":10,"time2_num":10,"time3_num":10}]}]
        $cardItem = json_decode($all['cardItem'], TRUE);
        foreach ($cardItem as $k => $v) {
            $item_name = $v['item_name'];
            $price = $v['price'];
            $special_price = $v['special_price'];
            $ser_num = $v['ser_num'];
            $factory_per = $v['factory_per'];
            $staff_per = $v['staff_per'];
            $store_per = $v['store_per'];
            $sell_per = $v['sell_per'];
            $kkm_per = $v['kkm_per'];

            $carditemData = [
                'card_id' => $insertCard,
                'item_name' => $item_name,
                'price' => $price,
                'special_price' => $special_price,
                'ser_num' => $ser_num,
                'factory_per' => $factory_per,
                'staff_per' => $staff_per,
                'store_per' => $store_per,
                'sell_per' => $sell_per,
                'kkm_per' => $kkm_per,
                'add_time' => date('Y-m-d H:i:s'),
            ];
            $insertCardItem = Db::name('super_card_item')->insertGetId($carditemData);
            //添加项目可约时间
            foreach ($v['yuyueTime'] as $key => $val) {
                $cardYuyueTimeData = [
                    'store_id' => $val['store_id'],
                    'card_item_id' => $insertCardItem,
                    'yuyue_date' => $val['yuyue_date'],
                    'time1_num' => $val['time1_num'],
                    'time2_num' => $val['time2_num'],
                    'time3_num' => $val['time3_num'],
                    'time1_surplus_num' => $val['time1_num'],
                    'time2_surplus_num' => $val['time2_num'],
                    'time3_surplus_num' => $val['time3_num'],
                    'add_time' => date('Y-m-d H:i:s'),
                ];
                $insertCardYuYueTime = Db::name('super_card_yuyue_time')->insertGetId($cardYuyueTimeData);
            }
        }
        if ($insertCard && $insertCardItem && $insertCardYuYueTime) {
            Db::commit();
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            Db::rollback();
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 上架/下架体验卡
     * author xfwed
     * date 2021-06-02
     */

    public function saveCardStatus() {
        $id = input('param.id');
        $status = input('param.status');
        $save = Db::name('super_card')->where('id', $id)->update(['status' => $status]);
        if ($save) {
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }

    /*
     * 查看体验卡
     * author xfwed
     * date 2021-06-02
     */

    public function cardInfo() {
        $id = input('param.id');
        $cardRow = Db::name('super_card')
                ->where('id', $id)
                ->find();
        $cardItemList = Db::name('super_card_item')
                ->where('card_id', $id)
                ->select();
        foreach ($cardItemList as $k => $v) {
            $card_item_id = $v['id'];
            $cardItemYuyueTime = Db::name('super_card_yuyue_time')
                    ->alias('t')
                    ->join('store s', 's.id = t.store_id', 'left')
                    ->where('t.card_item_id', $card_item_id)
                    ->field('t.*,s.store_name')
                    ->select();
            $cardItemList[$k]['cardItemYuyueTime'] = $cardItemYuyueTime;
        }
        $cardRow['cardItemList'] = $cardItemList;
        return $this->ajaxReturn(0, '查询成功', $cardRow);
    }

    /*
     * 体验卡订单列表
     * author xfwed 
     * date 2021.06.02
     */

    public function cardOrderList() {
        $name = input('param.name');
        $order_no = input('param.order_no');
        $custom_phone = input('param.custom_phone');
        $start_time = input('param.start_time');
        $end_time = input('param.end_time');
        $status = input('param.status');
        $enjoy_type = input('param.enjoy_type');
        $enjoy_name = input('param.enjoy_name');
        $perPage = input('param.perPage');
        $page = input('param.page');
        $card_id = input('param.card_id');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($name) {
            $where['sc.name'] = ['like', '%' . $name . '%'];
        }
        if ($order_no) {
            $where['sco.order_no'] = $order_no;
        }
        if ($custom_phone) {
            $where['c.custom_phone'] = $custom_phone;
        }
        if ($start_time && $end_time) {
            $where['sco.add_time'] = ['between', [$start_time, $end_time]];
        }
        if ($status) {
            if ($status != -1) {
                $where['sco.status'] = $status;
            }
        } else {
            if ($status == 0) {
                $where['sco.status'] = $status;
            }
        }
        if ($enjoy_type == 0) { //分享类型：-1全部 0平台 1.美容师 2.店铺 
            $where['sco.enjoy_type'] = 0;
        } elseif ($enjoy_type == 1) {
            $where['sco.enjoy_type'] = 1;
            $enjoyIds = Db::name('staff')->where('staff_name', 'like', '%' . $enjoy_name . '%')->column('id');
            $where['sco.enjoy_id'] = ['in', $enjoyIds];
        } elseif ($enjoy_type == 2) {
            $where['sco.enjoy_type'] = 2;
            $enjoyIds = Db::name('store')->where('store_name', 'like', '%' . $enjoy_name . '%')->column('id');
            $where['sco.enjoy_id'] = ['in', $enjoyIds];
        }
        if($card_id){
            $where['sco.card_id'] = $card_id;
        }

        $where = isset($where) ? $where : [];
        $list = Db::name('super_card_order')
                ->alias('sco')
                ->join('super_card sc', 'sc.id = sco.card_id', 'left')
                ->join('custom c', 'c.id = sco.ctm_id', 'left')
                ->where($where)
                ->field('sco.*,sc.name,c.custom_phone,c.custom_name,c.longitude,c.latitude')
                ->order('sco.id desc')
                ->paginate($perPage)
                ->each(function($item, $key) {
            //分享人
            $enjoy_type = $item['enjoy_type'];
            $enjoy_id = $item['enjoy_id'];
            if ($enjoy_type == 0) {
                $enjoy_name = '平台';
            } elseif ($enjoy_type == 1) {
                $enjoy_name = Db::name('staff')->where('id', $enjoy_id)->value('staff_name');
            } elseif ($enjoy_type == 2) {
                $enjoy_name = Db::name('store')->where('id', $enjoy_id)->value('store_name');
            }
            $item['enjoy_name'] = $enjoy_name;
            //查询用户的地址
            $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $item['latitude'] . "," . $item['longitude'] . "&key=ZJOBZ-PVNKD-MMV4P-HLV6T-X3BM3-Q3FBD";
            $relust = Utils::curlGet($url);
            $relust = json_decode($relust, true);
            $item['address'] = $relust['result']['address'];
            $item['city'] = $relust['result']['ad_info']['city'];
            return $item;
        });
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 体验卡订单详情
     * author xfwed 
     * date 2021.06.02
     */

    public function cardOrderInfo() {
        $id = input('param.id');

        $row = Db::name('super_card_order')
                ->alias('sco')
                ->join('super_card sc', 'sc.id = sco.card_id', 'left')
                ->join('custom c', 'c.id = sco.ctm_id', 'left')
                ->where('sco.id', $id)
                ->field('sco.*,sc.name,sc.img,sc.price,sc.special_price,c.custom_phone,c.custom_name,c.longitude,c.latitude')
                ->order('sco.id desc')
                ->find();
        //分享人
        $enjoy_type = $row['enjoy_type'];
        $enjoy_id = $row['enjoy_id'];
        if ($enjoy_type == 0) {
            $enjoy_name = '平台';
            $enjoy_city = '###';
        } elseif ($enjoy_type == 1) {
            $enjoy_name = Db::name('staff')->where('id', $enjoy_id)->value('staff_name');
            $city_id = Db::name('staff')->where('id', $enjoy_id)->value('city');
            $enjoy_city = Db::name('prov_city_area')->where('id', $city_id)->value('merger_name');
        } elseif ($enjoy_type == 2) {
            $enjoy_name = Db::name('store')->where('id', $enjoy_id)->value('store_name');
            $city_id = Db::name('store')->where('id', $enjoy_id)->value('city');
            $enjoy_city = Db::name('prov_city_area')->where('id', $city_id)->value('merger_name');
        }
        $row['enjoy_name'] = $enjoy_name;
        $row['enjoy_city'] = $enjoy_city;
        //查询用户的地址
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $row['latitude'] . "," . $row['longitude'] . "&key=ZJOBZ-PVNKD-MMV4P-HLV6T-X3BM3-Q3FBD";
        $relust = Utils::curlGet($url);
        $relust = json_decode($relust, true);
        $row['address'] = $relust['result']['address'];
        $row['city'] = $relust['result']['ad_info']['city'];

        return $this->ajaxReturn(0, '查询成功', $row);
    }

    /*
     * 体验卡预约列表
     * author xfwed 
     * date 2021.06.03
     */

    public function cardYuyueList() {
        $name = input('param.name');
        $order_no = input('param.order_no');
        $custom_phone = input('param.custom_phone');
        $start_time = input('param.start_time');
        $end_time = input('param.end_time');
        $status = input('param.status');
        $item_name = input('param.item_name');
        $store_name = input('param.store_name');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($name) {
            $where['sc.name'] = ['like', '%' . $name . '%'];
        }
        if ($order_no) {
            $where['sco.order_no'] = $order_no;
        }
        if ($custom_phone) {
            $where['c.custom_phone'] = $custom_phone;
        }
        if ($start_time && $end_time) {
            $where['scy.yuyue_date'] = ['between', [$start_time, $end_time]];
        }
        if ($status) {
            $where['scy.status'] = $status;
        }
        if ($item_name) {
            $where['sci.item_name'] = ['like', '%' . $item_name . '%'];
        }
        if ($store_name) {
            $where['s.store_name'] = ['like', '%' . $store_name . '%'];
        }

        $where = isset($where) ? $where : [];

        $list = Db::name('super_card_yuyue')
                ->alias('scy')
                ->join('super_card_order sco', 'sco.id = scy.order_id', 'left')
                ->join('super_card sc', 'sc.id = sco.card_id', 'left')
                ->join('super_card_item sci', 'sci.id = scy.item_id', 'left')
                ->join('custom c', 'c.id = sco.ctm_id', 'left')
                ->join('store s', 's.id = scy.store_id', 'left')
                ->where($where)
                ->field('scy.id,scy.yuyue_date,scy.yuyue_time,scy.handle_yuyue_time,scy.status,scy.note,scy.cancel_time,scy.cancel_why,scy.ser_time,sco.order_no,sco.enjoy_type,sco.enjoy_id,sc.name cardName,sci.item_name,s.store_name,c.custom_phone,c.custom_name')
                ->order('scy.id desc')
                ->paginate($perPage)
                ->each(function($item, $key) {
            //分享人
            $enjoy_type = $item['enjoy_type'];
            $enjoy_id = $item['enjoy_id'];
            if ($enjoy_type == 0) {
                $enjoy_name = '平台';
            } elseif ($enjoy_type == 1) {
                $enjoy_name = Db::name('staff')->where('id', $enjoy_id)->value('staff_name');
            } elseif ($enjoy_type == 2) {
                $enjoy_name = Db::name('store')->where('id', $enjoy_id)->value('store_name');
            }
            $item['enjoy_name'] = $enjoy_name;
            return $item;
        });

        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 结束活动
     * author xfwed 
     * date 2021.06.07
     */

    public function activityEnd() {
        $card_id = input('param.card_id');

        $cardRow = Db::name('super_card')
                ->where('id', $card_id)
                ->find();
        //判断体验卡是否已经到期
        $end_time = strtotime($cardRow['end_time']) + 24 * 60 * 60;
        if ($end_time > time()) {
            return Utils::ajaxReturn(3, '该体验卡活动还没有结束', null);
        }
        $AppPath = config('appconfig.AppPath');
        $postFields['card_id'] = $card_id;
        return Utils::curlPost1($AppPath . 'Experience/activityEnd', $postFields);
    }

}
